/**
 * javascript Object转换为 json
 * @param o
 * @returns
 */
function obj2json(o) {
	var r = [];
	if (typeof o == "string")
		return "\""
				+ o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n")
						.replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
	if (typeof o == "object") {
		if (!o.sort) {
			for ( var i in o)
				r.push(i + ":" + obj2json(o[i]));
			if (!!document.all
					&& !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/
							.test(o.toString)) {
				r.push("toString:" + o.toString.toString());
			}
			r = "{" + r.join() + "}";
		} else {
			for ( var i = 0; i < o.length; i++)
				r.push(obj2json(o[i]));
			r = "[" + r.join() + "]";
		}
		return r;
	}
	return o.toString();
}



/*form 转换为json 字符串*/

$.fn.form2json = function () {  
    var o = {};  
    var a = this.serializeArray();  
    $.each(a, function () {  
        if (o[this.name]) {  
            if (!o[this.name].push) {  
                o[this.name] = [o[this.name]];  
            }  
            o[this.name].push(this.value || '');  
        } else {  
            o[this.name] = this.value || '';  
        }  
    });   
    return obj2json(o);  
};  

/** 对easyui datagrid 的封装 */

/**
 *  datagrid多项选择函数
 *  @returns ids
 */
function getSelections(){
	var ids = [];
	var rows = $('#grid').datagrid('getSelections');
	if(rows.length==0){
		return "";
	}
	for(var i=0;i<rows.length;i++){
		ids.push(rows[i].id);
	}
	return ids.join(',');
}

/**
 * datagrid选择单项验证函数
 * @returns boolean
 */
function isSelectOne(){
	var rows = $('#grid').datagrid('getSelections');
	if(rows.length>1){
		return true;
	}else{
		return false;
	}
}

/**
 *  datagrid单项选择函数
 *  @returns id
 */
function getSelection(){	
	var selected = $('#grid').datagrid('getSelected');
	if (selected){
		return selected.id;
	}else{
		return "";
	}
}


function clearSelections(){
	$('#grid').datagrid('clearSelections');
}


/** easyui 消息的封装 */
function alertInfo(msg,title){
	if(title==null||title=='')
		$.messager.alert('消息','<br/>'+msg,'info');
	else
	    $.messager.alert(title,'<br/>'+msg,'info');
}

function alertError(msg,title){
	if(title==null||title=='')
		$.messager.alert('错误','<br/>'+msg,'error');
	else
		$.messager.alert(title,'<br/>'+msg,'error');
}

function alertQuestion(msg,title){
	if(title==null||title=='')
		$.messager.alert('问题','<br/>'+msg,'question');
	else
		$.messager.alert(title,'<br/>'+msg,'question');
}

function alertWarn(msg,title){	
	 if(title==null||title=='')
		 $.messager.alert('警告','<br/>'+msg,'warning');
	 else
		 $.messager.alert(title,'<br/>'+msg,'warning');
}

function econfirm(title,msg,callback){
	$.messager.confirm(title,'<br/>'+msg,callback); 
}

function prompt(title,msg,callback){  
    $.messager.prompt(title,'<br/>'+msg,callback);  
} 

function showSlide(title,msg,time){
	if(time==''||time==null){
		$.messager.show({  
	        title:'消息',  
	        msg:msg,  
	        timeout:4000,  
	        showType:'slide'  
	    });
	}else{
	    $.messager.show({  
	        title:title,  
	        msg:'<br/>'+msg,  
	        timeout:time,  
	        showType:'slide'  
	    });
	}
}

function winopen(url,name){
	if(url.indexOf('#')!=-1){
		return;
	}
	if(url.indexOf('www')!=-1||url.indexOf('.gov')!=-1||url.indexOf('.edu')!=-1||url.indexOf('.com')!=-1||url.indexOf('.cn')!=-1||url.indexOf('.net')!=-1){
		if(url.indexOf('http://')==-1){
			url='http://'+url;
		}
	}
	if(name==null){
		name='';
	}
	window.open(url,name,'');
}

function makeTree(url,divId,callback){
	url = url +"&iframeId_="+iframeId_+"&tabId_="+tabId_+"&datasrcId_="+datasrcId_;
	var setting = {
            data: {
            	key:{
					name:'text'
				},
                    simpleData: {
                            enable: true,
                            idKey: "id",
                            pIdKey: "pId",
                            rootPId: 0
                    }
            },
            async: { 
                    type: "post",
                    enable: true,
                    url:url
            },
            callback : {
                onClick: callback
                /* onClick: function(event,treeId,node){
				
			      }*/
            }
    };
    $.fn.zTree.init($("#"+divId), setting);//初始化树对象a
}

function makeCheakTree(url,divId,callback){
	 url = url +"&iframeId_="+iframeId_+"&tabId_="+tabId_+"&datasrcId_="+datasrcId_;
	 var setting = {
			 check: {
			    enable: true,
			    chkStyle: "checkbox",
			    chkboxType: { "Y" : "ps", "N" : "ps" }
			 },
			  data: {
			  	key:{
						name:'text'
					},
			      simpleData: {
			              enable: true,
			              idKey: "id",
			              pIdKey: "pId",
			              rootPId: 0
			      }
			  },
			  async: { 
			          type: "post",
			          enable: true,
			          url: url
			  },
			  callback : {
				  onClick:callback
			     /* onClick: function(event,treeId,node){
						
			      }*/
			  }
			
			};
			$.fn.zTree.init($("#"+divId), setting);//初始化树对象a
}

var uploadLeadPic=function(fileDir,valField,fileSelField,showField)
{
	 var uploadFile = dwr.util.getValue(fileSelField); 
	 var uploadFileuploadFile_temp = uploadFile.value.replace("\\","/");  			 
	 var filenames = uploadFile.value.split("\\");   
	 var filename = filenames[filenames.length-1];  
	 if(filename==''){ 
		 $.messager.alert('系统提示:','<br>    请先选择需上传的文件!','warning');
		 return;
	 }
	 else
	 {	
		 var fileType =  filename.substring(filename.lastIndexOf(".")+1); 
		 fileType = fileType.toLowerCase();
		 if( fileType !='jpg' && fileType !='jpeg'&& fileType !='png'&& fileType !='gif')
		  {
			 $.messager.alert('系统提示:','请选择jpg,jpeg类型图片文件上传!','warning');
			 return;
		  }			 
		dwrService.uploadFile2Dir(uploadFile,  fileDir ,filename,function(data){   				
			if(data=='')
			{
				$.messager.alert('系统提示：','<br>     文件上传失败!','error');
			}
			else
			{
				dwr.util.setValue(valField,data);			
				if(showField){ 
					$('#' + showField).attr('src',contextPath + data);
				}
				if(valField){
					$('#' + valField).val(data);
				}
				$.messager.alert('系统提示：','<br>    文件上传成功!','info');					
			}
	    });   
	 }
};

var uploadFile=function(fileDir,valField,fileSelField,showname)
{
	 var uploadFile = dwr.util.getValue(fileSelField); 
	 var uploadFileuploadFile_temp = uploadFile.value.replace("\\","/");  			 
	 var filenames = uploadFile.value.split("\\");   
	 var filename = filenames[filenames.length-1];  
	 if(filename==''){ 
		 $.messager.alert('系统提示:','<br>    请先选择需上传的文件!','warning');
		 return;
	 }
	 else
	 {	
		dwrService.uploadFile2Dir(uploadFile,  fileDir ,filename,function(data){   				
			if(data=='')
			{
				$.messager.alert('系统提示：','<br>     文件上传失败!','error');
			}
			else
			{
				dwr.util.setValue(valField,data);			
				if(showname){ 
					filename = filename.substring(0,filename.lastIndexOf("."));
					$('#' + showname).val(filename);
				}
				if(valField){
					$('#' + valField).val(data);
				}
				$.messager.alert('系统提示：','<br>    文件上传成功!','info');					
			}
	    });   
	 }
};
  